<?php

namespace App\Http\Controllers\Backstage;

use App\Http\Controllers\Backstage\BCommonController;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;
use App\Models\BackstageModel;
use App\Models\CommonModel;
use Storage;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Libary\Captcha\Code;

// require_once '../app/libary/captcha/Code.class.php';

class RechargeController extends BCommonController
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/backstage/recharge/index';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    // public function __construct()
    // {
    //     $this->middleware('guest', ['except' => 'logout']);
    // }

    public function index(){
        // DB::connection()->enableQueryLog(); // 開啟查詢日誌
        if(isset($_GET['keywords'])){
            $keywords = trim($_GET['keywords']);
            // $keywords = '使用';
            $list = DB::table(DB::raw('zzb_recharge r'))->select(DB::raw('r.id,r.uid,u.user_name,u.nickname,r.money,r.status,r.add_time,r.time_end,m.account_money'))->leftJoin(DB::raw('zzb_user u'), DB::raw('u.id'), '=', DB::raw('r.uid'))->leftJoin(DB::raw('zzb_user_money m'), DB::raw('m.uid'), '=', DB::raw('r.uid'))->where(DB::raw('u.user_name'),'like','%'.$keywords.'%')->orderBy(DB::raw('r.id'),'desc')->paginate(15);
        }else{
            $list = DB::table(DB::raw('zzb_recharge r'))->select(DB::raw('r.id,r.uid,u.user_name,u.nickname,r.money,r.status,r.add_time,r.time_end,m.account_money'))->leftJoin(DB::raw('zzb_user u'), DB::raw('u.id'), '=', DB::raw('r.uid'))->leftJoin(DB::raw('zzb_user_money m'), DB::raw('m.uid'), '=', DB::raw('r.uid'))->orderBy(DB::raw('r.id'),'desc')->paginate(15);
        }
        $recharge_status = config('app.recharge_status');
// dd(count($list));exit;
// DB::table('order'); // 要查看的sql

// $queries = DB::getQueryLog(); // 獲取查詢日誌

// print_r($queries);exit; // 即可查看執行的sql，傳入的慘數等等
        return view('backstage/recharge/index',['list'=>$list,'recharge_status'=>$recharge_status]);
    }
    public function edit($id){
        $table = 'recharge';
        $data =['id'=>$id];
        $recharge = CommonModel::getOne($table,$data);
        $recharge_status = config('app.recharge_status');
        $recharge->bank_name = config('app.bank_type')[$recharge->bank_id];
        $recharge->remit_type = config('app.remit_type')[$recharge->remit_type];
        return view('backstage/recharge/edit',['recharge'=>$recharge,'recharge_status'=>$recharge_status]);
    }
    public function doedit(Request $request){
        if($request->isMethod('POST')){
            $data = $request->all();
            $id = $data['id'];
            $recharge['status'] = intval($data['status']);
            $rechargeInfo = CommonModel::getOne('recharge',['id'=>$id]);
            $recharge_status = config('app.recharge_status');
            $save = CommonModel::updateData('recharge',['id'=>$id],$recharge);
            if($save == true){
                $logContent = "将充值状态由:<b style='color:red'>".$recharge_status[$orderInfo->status]."</b>,修改为:<b style='color:red'>".$recharge_status[$recharge['status']]."</b>。";
                saveCommonLog($id, $logContent, 'recharge', session('admin')->real_name, session('admin')->id, 6);
                if($recharge['status']==1){
                    userMoneyLog($rechargeInfo->uid, 2, $rechargeInfo->money, $data['id'],'用戶充值成功');
                    // 完成時間
                    CommonModel::updateData('recharge',['id'=>$id],['time_end'=>time() ]);
                }
                $arrJson = array('status' => 1, 'message'=>'操作成功','jumpUrl'=>$this->redirectTo);
            }else{
                $arrJson = array('status' => 0, 'message'=>'操作失敗');
            }
            return json_encode($arrJson);
        }
    }


}
